package com.zp.self.module.level_4_算法练习.数据结构.String;

import com.google.common.collect.Lists;

import java.util.Comparator;
import java.util.List;

/**
 * @author By ZengPeng
 * @Description 在英语中，我们有一个叫做 词根(root)的概念，它可以跟着其他一些词组成另一个较长的单词——我们称这个词为 继承词(successor)。例如，词根an，跟随着单词 other(其他)，可以形成新的单词 another(另一个)。
 *              现在，给定一个由许多词根组成的词典和一个句子。你需要将句子中的所有继承词用词根替换掉。如果继承词有许多可以形成它的词根，则用最短的词根替换它。
 *              你需要输出替换之后的句子。
 *
 * @date in  2021/1/20 19:13
 * @Modified By
 */
public class 力扣_648_单词替换 {
    public static void main(String[] args) {
        System.out.println(myAnswer(Lists.newArrayList("cat","bat","rat"),"the cattle was rattled by the battery"));
        System.out.println(myAnswer(Lists.newArrayList("a","b","c"),"aadsfasf absbs bbab cadsfafs"));
        System.out.println(myAnswer(Lists.newArrayList("a", "aa", "aaa", "aaaa"),"a aa a aaaa aaa aaa aaa aaaaaa bbb baba ababa"));
        System.out.println(myAnswer(Lists.newArrayList("catt","cat","bat","rat"),"the cattle was rattled by the battery"));
        System.out.println(myAnswer(Lists.newArrayList("ac","ab"),"it is abnormal that this solution is accepted"));
    }

    public static String myAnswer(List<String> dictionary, String sentence){
        dictionary.sort(Comparator.comparingInt(String::length));
        String[] split = sentence.split(" ");
        for (int i = 0; i < split.length; i++) {
            for (String s1 : dictionary) {
                //其实就是一个个字符相比较啊 ，我去
                if (split[i].startsWith(s1))
                    split[i] = s1;
            }
        }
        return String.join(" ", split);
    }
}
